import { State } from "framework/FSM/AState";
import { ActionArgHelper } from "../../../ActionManager/ActionArgHelper";
import { EntityHelper } from "utils/entity/EntityHelper";
import { InnerMineEntity, MinerialEntity } from "GameLoop/WorldState/EntityManager/Entitys";
import { ActionUtils } from "../utils/ActionUtil";
import { EActionCode } from "framework/FSM/Action/ActionCode";
 

/**
 * 采集任务
 */ 
export class HarvestMinerial<T extends IStateArg<Creep>>  extends State<T>
{

    //
    public  getActions(arg:T):Action[]
    {
       //global.Log.Info(`${arg.entity.name} 采集任务`);
        const creep = arg.entity;
        if(!creep.memory.taskid)
        {

            return []
        }
        const task =arg.task as ITask<ICreepTaskData<"harvestM">>;
        //const task = arg.entity.ownerRoom().findTaskById<ITask<ICreepTaskData<"harvestM">>>(creep.memory.taskid,"creep");
        if(!task)
        {
            //global.Log.Info(`${arg.entity.name} 采集任务 1`);
            return []
        }
        const bindMine = arg.entity.getEntityByID<MinerialEntity>(task.data.arg.entityId);
        const actions = ActionUtils.getEscapeSourceKeeperAction(creep,bindMine.BasePosition);
        if(actions)
        {
            return actions
        }
       // global.Log.Info(`${arg.entity.name} -采集2`)
        
        if(creep.room.name == bindMine.roomName)
        {
            // 采集
            const container = bindMine.getContainer();
            if(container   )
            {
                if(container.pos.isEqualTo(creep.pos))
                {
                    if(container.store.getFreeCapacity() ==0)
                    {
                        creep.log("warn",'容器已满等待搬运');
                        // creep.room.
                    }
                    else
                    {
                        if(creep.room.getType() == 6)
                        {
                            return [ActionArgHelper.warp( ActionArgHelper.actionHarvest(creep,bindMine.getMinerial()),undefined,(arg)=>
                                {
                                    if(ActionUtils.isFlee(arg.arg.entity))
                                    {
                                        // 已经过期了
                                        return true;
                                    }
                                    return false
                                })]
                        }
                        else 
                        {
                            return [ 
                                ActionArgHelper.actionHarvest(creep,bindMine.getMinerial())]
                        }
                      
                    }
                    
                }
                else
                {
                    return [ActionArgHelper.actionMoveto(creep,{pos:bindMine.getWorkPosition(),range:0},{}) ]
                }
                
            }
            else
            {
              
                if(creep.store.getFreeCapacity()>0)
                {
                    if(creep.room.getType() == 6)
                    {
                        return [ActionArgHelper.warp( ActionArgHelper.actionHarvest(creep,bindMine.getMinerial()),undefined,(arg)=>
                            {
                                
                                if(creep.room.canExecute(5) && ActionUtils.isFlee(arg.arg.entity))
                                {
                                    // 已经过期了
                                    return true;
                                }
                                return false
                            })]
                    }
                    else 
                    {
                        return [ 
                            ActionArgHelper.actionHarvest(creep,bindMine.getMinerial())]
                    }
                }
                else 
                {
                    // creep.log("warn",'找不到容器 会造成资源丢失');
                }
              
            }
             
        }
        else
        {
            
           return [ActionArgHelper.actionMoveto(creep,{pos:bindMine.BasePosition,range:1},{})]
        }
        return []
    }
}
